<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #p>div{
            width: 120px;
            height: 120px;
            background-color: black;
            float: left;
            margin-left: 10px;
        }
    </style>
</head>
<body>
    <button>将div变为红色</button>
    <div id="p">
        <div></div>
        <div></div>
        <div></div>
    </div>
    <script>

window.onload = function ()
{
	var pArr = document.getElementById("p").getElementsByTagName("div");
	var but = document.getElementsByTagName("button")[0];
	but.onclick = function ()
	{
		for(var i = 0; i < pArr.length; i++){
            pArr[i].style.background = "red";
	}	
        }
}
        





    </script>
    
</body>
</html>